<template>
  <div class="view-category-container">
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="header-box">
        <span class="header-title">查看分类</span>
      </div>

      <el-form
        :model="formData"
        label-width="100px"
        class="form-container"
        size="small"
      >
        <el-form-item label="所属小区" prop="communityId" required>
          <el-input
            v-model="formData.communityName"
            class="compact-input"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="分类名称" required>
          <el-input
            v-model="formData.categoryName"
            class="compact-input"
            disabled
          >
          </el-input>
        </el-form-item>

        <el-form-item label="描述">
          <el-input
            v-model="formData.remark"
            type="textarea"
            :rows="2"
            class="compact-input"
            disabled
          >
          </el-input>
        </el-form-item>

        <el-form-item label="分类图标">
          <div class="avatar-container">
            <img v-if="formData.url" :src="formData.url" class="avatar" />
            <div v-else class="no-avatar">
              <i class="el-icon-picture-outline"></i>
              <div>暂无图标</div>
            </div>
          </div>
        </el-form-item>

        <el-form-item label="是否显示">
          <el-radio-group
            v-model="formData.isDelete"
            class="custom-radio-group"
            disabled
          >
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="formData.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            disabled
          >
          </el-switch>
          <span class="status-text">{{
            formData.status === 1 ? "启用" : "禁用"
          }}</span>
        </el-form-item>
      </el-form>

      <div class="button-group">
        <el-button type="primary" @click="handleEdit">
          <i class="el-icon-edit"></i>
          编辑
        </el-button>
        <el-button @click="handleCancel">
          <i class="el-icon-back"></i>
          返回
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categoryId: null,
      formData: {
        communityId: "",
        communityName: "",
        categoryName: "",
        remark: "",
        url: "",
        isDelete: 1,
        status: 1,
      },
      loading: {
        category: false
      }
    };
  },

  created() {
    // 获取路由参数中的ID
    this.categoryId = this.$route.query.id;
    if (!this.categoryId) {
      this.$message.error('缺少分类ID参数');
      this.$router.go(-1);
      return;
    }
    
    // 获取分类详情
    this.fetchCategoryDetail();
  },
  
  methods: {
    // 获取分类详情
    fetchCategoryDetail() {
      this.loading.category = true;
      this.$axios.get(`/api/patrol-server/patrolCategory/getById/${this.categoryId}`)
        .then(response => {
          if (response.data && response.data.code === 200) {
            // 填充表单数据
            const categoryData = response.data.data;
            this.formData = {
              ...categoryData,
              communityId: categoryData.communityId,
              communityName: categoryData.communityName,
              // 确保布尔值正确设置
              isDelete: categoryData.isDelete || 1,
              status: categoryData.status || 1
            };
          } else {
            this.$message.error(response.data.msg || '获取分类详情失败');
            setTimeout(() => {
              this.$router.go(-1);
            }, 1500);
          }
        })
        .catch(error => {
          console.error('获取分类详情错误:', error);
          this.$message.error('网络异常，获取分类详情失败');
          setTimeout(() => {
            this.$router.go(-1);
          }, 1500);
        })
        .finally(() => {
          this.loading.category = false;
        });
    },

    // 点击编辑按钮
    handleEdit() {
      this.$router.push({
        path: "/patrolCategoryEdit",
        query: { id: this.categoryId }
      });
    },

    // 点击返回按钮
    handleCancel() {
      this.$router.go(-1);
    }
  },
};
</script>

<style scoped>
.view-category-container {
  padding: 20px;
  width: 100%;
  max-width: 650px;
  margin: 0 auto;
}

.box-card {
  border-radius: 8px;
  transition: all 0.3s ease;
}

.box-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
}

.header-box {
  display: flex;
  align-items: center;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  position: relative;
  padding-left: 12px;
}

.header-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 16px;
  width: 4px;
  background-color: #409eff;
  border-radius: 2px;
}

.form-container {
  margin: 12px 0;
  padding: 0 10px;
}

/* 紧凑型输入框样式 */
.compact-input {
  width: 280px !important;
  transition: all 0.3s ease;
}

.compact-input .el-input__inner {
  height: 32px;
  line-height: 32px;
  font-size: 13px;
}

.compact-input .el-textarea__inner {
  font-size: 13px;
  padding: 8px 10px;
}

.el-form-item {
  margin-bottom: 16px;
}

/* 头像容器样式 */
.avatar-container {
  width: 140px;
  height: 140px;
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid #ebeef5;
}

.avatar {
  width: 140px;
  height: 140px;
  display: block;
  object-fit: cover;
}

.no-avatar {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #f7f9fc;
  color: #909399;
}

.no-avatar i {
  font-size: 40px;
  margin-bottom: 10px;
}

.status-text {
  margin-left: 10px;
  font-size: 14px;
  color: #606266;
}

.custom-radio-group {
  display: flex;
  align-items: center;
}

.button-group {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

.button-group .el-button {
  margin: 0 15px;
  padding: 8px 20px;
  min-width: 100px;
  font-size: 13px;
  transition: all 0.3s ease;
}

.button-group .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.button-group .el-button i {
  margin-right: 5px;
}

@media screen and (max-width: 768px) {
  .view-category-container {
    padding: 10px;
  }
  
  .form-container {
    padding: 0;
  }
  
  .compact-input {
    width: 100% !important;
  }
}
</style>